package chapter11;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 使用适配器来实现监听，相比接口更加方便，只重写自己感兴趣的方法即可
 */
public class WindowAdapterTest {
    private Frame frame = new Frame("测试");
    private TextArea ta = new TextArea(6, 40);

    public static void main(String[] args) {
        new WindowAdapterTest().init();
    }

    public void init() {
        frame.add(ta);
        frame.addWindowListener(new MyListener());
        frame.pack();
        frame.setVisible(true);
    }

    class MyListener extends WindowAdapter {
        @Override
        public void windowClosing(WindowEvent e) {
            ta.append("用户关闭窗口\n");
            System.exit(0);
        }
    }
}
